package com.googlecode.animator.view.workarea
{
	import com.googlecode.animator.data.Notifications;
	import com.googlecode.data.stage.StagePropertiesVO;
	
	import mx.containers.Canvas;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class WorkAreaMediator extends Mediator
	{
		/**
		 * Produces a unique number to be appended to this mediator's name.
		 */
		public static var count:int = 0;
		
		/**
		 * Identifier. 
		 */
		public static const NAME:String = "workAreaMediator";
		
		/**
		 * Constructor. 
		 * @param viewComponent
		 * 
		 */
		public function WorkAreaMediator(viewComponent:Canvas)
		{
			var name:String = NAME + "_" + count;
			
			super(name, viewComponent);
						
			count ++;
		}
		
		override public function onRegister():void
		{
			var stageObj:StagePropertiesVO = new StagePropertiesVO();
			
			sendNotification(Notifications.SET_STAGE_SIZE_MODEL, stageObj);
			sendNotification(Notifications.SET_WORK_AREA_VISIBLE, stageObj);
			
		}
		
		/**
		 * Lists notification of interests. 
		 * @return 
		 * 
		 */
		override public function listNotificationInterests():Array
		{
			var list:Array = super.listNotificationInterests();
			
			list.push(Notifications.STAGE_WIDTH_UPDATED_MODEL);
			list.push(Notifications.STAGE_HEIGHT_UPDATED_MODEL);
			list.push(Notifications.SET_WORK_AREA_VISIBLE);
			
			return list;
		}
		
		/**
		 * Handles notification of interests. 
		 * @param notification
		 * 
		 */
		override public function handleNotification(notification:INotification):void
		{
			super.handleNotification(notification);

			switch(notification.getName())
			{
					
				case Notifications.STAGE_WIDTH_UPDATED_MODEL:
				
					handleStageWidthUpdated(notification.getBody() as StagePropertiesVO);
					
					break;
				
				case Notifications.STAGE_HEIGHT_UPDATED_MODEL:
				
					handleStageHeightUpdated(notification.getBody() as StagePropertiesVO);
					
					break;
				
				case Notifications.SET_WORK_AREA_VISIBLE:
				
					handleSetWorkAreaVisible(notification.getBody() as StagePropertiesVO);
					
					break;
					
			}
		}
		
		
		protected function handleStageWidthUpdated(VO:StagePropertiesVO):void
		{
			viewRef.width = VO.stageWidth;
		}

		protected function handleStageHeightUpdated(VO:StagePropertiesVO):void
		{
			viewRef.height = VO.stageHeight;
		}
		
		protected function handleSetWorkAreaVisible(VO:StagePropertiesVO):void
		{
			viewRef.visible = VO.workAreaVisible;
		}
		
		/**
		 * Returns a typed reference to the ZoomControl component. 
		 * @return 
		 * 
		 */
		protected function get viewRef():Canvas
		{
			return viewComponent as Canvas;
		}
		
	}
}